package com.example.datn.favorite;



import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.preference.PreferenceActivity.Header;

public class MyDataHelper {
	
	public static final String DB_NAME = "danhsachyeuthich.db";
	public static final String TB_NAME = "yeuthich";
	public static final int DB_VERSION = 1;
	private SQLiteDatabase database;
	private Context mContext;
	
	
	public MyDataHelper(Context context){
		this.mContext = context;
		
		
		MyOpenHelper helper = new MyOpenHelper(mContext);
		
		database = helper.getWritableDatabase();  
	}
	
	
	public void insertDB(String Title, String Link){
		ContentValues values = new ContentValues();
		values.put("Title", Title);
	values.put("Link", Link);
	
		
		
		
		
		
		database.insertOrThrow(TB_NAME, null, values);
	}
	
	/*update*/
	public void updateDB(String Title,int id){
		ContentValues values = new ContentValues();
		values.put("Title", Title);
		
		database.update(TB_NAME, values, "_id = " + id, null);
	}
	
	/*delete*/
	public void deleteDB(int id){
		database.delete(TB_NAME, "_id = " + id, null);
	}
	
	/*select*/
	public Cursor selectAll(){
		return database.query(TB_NAME, null, null, null, null, null, null);
	}
	
	/*update co dieu kien*/
	public void updateDBCondition(String name, String selecArg){
		ContentValues values = new ContentValues();
		values.put("Title", name);
		
		database.update(TB_NAME, values, "_id > ? " , new String[]{selecArg});
	}
	
	/**
	 * tao 1 lop noi de thuc hien viec dong bo hoa he thong co so du lieu
	 */
	public class MyOpenHelper extends SQLiteOpenHelper{

		public MyOpenHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// tao ra cac bang de luu tru
			db.execSQL("CREATE TABLE IF NOT EXISTS yeuthich(_id INTEGER PRIMARY KEY AUTOINCREMENT,Title TEXT,Link TEXT)");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// thuc hien viec update du lieu
			db.execSQL("drop table if exists yeuthich");  /*xoa bang di*/
			onCreate(db);                                /*tao bang moi*/
		}
		
		
		
		
		
	}
	
		
	
	
	
	
	
	
}
